<?php
/**
 * 房间，健身房，温泉
 * User:zhan
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\Controller;


use think\Db;
use think\Request;

class Goods extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}
	/**
	 * 列表
	 */
	public function index(){
		$room = db('hotel_room');
		$where['r.delete']=1;
		$where['p.roomdate']=strtotime(date("Y-m-d"));
		if(input('type')!=''){
			$where['type']=input('type');
		}
		if(input('status')!=''){
			$where['r.status']=input('status');
		}
		if(input('keyword')!=''){
			$where['r.title']=array('like','%'.input('keyword').'%');
		}
		$list = $room->alias("r")->where($where)
				->field("r.*,p.oprice,p.num,p.price1,p.price2,p.price4,p.price3")
				->join("hotel_room_price p","p.roomid=r.id")
				->order("r.sort desc,r.id desc")
				->paginate(8);

		$this -> assign("list",$list);
		$this -> assign("status",input('status'));
		$this -> assign("type",input('type'));
		$this->assign('keyword',input('keyword'));
		return $this->fetch();
	}
	/**
	 * 添加或修改
	 */
	public function post(){
		$id = input('id');
		if(Request::instance()->isPost()){
			$data['title'] = input('title');
			if(!$data['title'])$this->error("标题不能为空!");
			$data['sort'] = intval(input('sort'));
			$data['type'] = intval(input('type'));
			$data['content']=input('content');
			$data['des']=serialize(input('des/a'));
			$file = request()->file('image');
			if (!empty($file)) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['image'] = $info->getSaveName();
				}
			}
			$key=input("key/a");
			$param=input("param/a");
			if(!$key) $this->error("添加属性");
			$option=array();
			foreach($key as $k=>$v){
				$option[$k]['key']=$v;
				$option[$k]['value']=$param[$k];
			}
			$data['option']=serialize($option);
			$files = request()->file('photoname');
			$images=input('photo/a');
			if (!empty($files)) {
				foreach($files as $file){
                        // 移动到框架应用根目录/public/uploads/ 目录下
                        $info = $file->move(ROOT_PATH . 'public/static/uploads');
                        if($info){
							if($images) {
								array_push($images, $info->getSaveName());
							}else{
								$images[] = $info->getSaveName();
							}
                        }
                    }
			}
			if($images){
				$data['images']=serialize($images);
			}
			$price1=input('price1');
			$price2=input('price2');
			$price3=input('price3');
			$price4=input('price4');
			$num=input('num');
			$oprice=input('oprice');
			//var_dump($price1);exit;
			if($id){
				$res = db('hotel_room')->where("id='$id'")->update($data);
			}else{
				$id=db('hotel_room')->insertGetId($data);
				$res=$this->getMonth($id,$price1,$price2,$price3,$price4,$num,$oprice);
			}

			if($res!==false){
				$this->redirect(url("goods/index"));
				$this->success("操作成功!",url("goods/index"));
			}else{
				$this->error("操作失败!");
			}
		}

		$where['id']=$id;
		$where['delete']=1;
		$room=db('hotel_room')->where($where)->find();
		if($room['option']){
			$room['option']=unserialize($room['option']);
		}
		if($room['images']) {
			$room['images'] = unserialize($room['images']);
		}
		if($room['des']) {
			$room['des'] = unserialize($room['des']);
		}
		$this -> assign("data",$room);
		return $this->fetch();

	}
	/**
	 * 删除，上下架
	 */
	public function batch()
	{
		$batch=input('box/a');
		if(!$batch) $this->error('请选择商品');
		$id = implode(",",$batch);
		$where['id'] = array("in", $id);
		if(input("type/d")==0) {
			$res = db('hotel_room')->where($where)->update(array("delete"=>0));
		}
		if(input("type/d")==2) {
			$res = db('hotel_room')->where($where)->update(array("status"=>0));
		}
		if(input("type/d")==1) {
			$res = db('hotel_room')->where($where)->update(array("status"=>1));
		}
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	/**
	 * 删除，上下架
	 */
	public function batch2()
	{
		$batch=input('box/a');
		if(!$batch) $this->error('请选择商品');
		$id = implode(",",$batch);
		$where['id'] = array("in", $id);
		if(input("type/d")==0) {
			$res = db('package')->where($where)->update(array("delete"=>0));
		}
		if(input("type/d")==2) {
			$res = db('package')->where($where)->update(array("status"=>0));
		}
		if(input("type/d")==1) {
			$res = db('package')->where($where)->update(array("status"=>1));
		}
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	/**
	 * 添加或修改
	 */
	public function packagepost(){
		$id = input('id');
		$p['type']= intval(input('type'));
		if(!in_array($p['type'],array(0,1))) $this->error("参数有误!");
		if(Request::instance()->isPost()){
			$data['title'] = input('title');
			if(!$data['title']) $this->error("标题不能为空!");
			$data['sort'] = intval(input('sort'));
			$data['type'] = intval(input('type'));
			$data['status'] = intval(input('status'));
			$data['content']=input('content');
			$data['price']=floatval(input('price'));
			$data['oprice']=floatval(input('oprice'));
			if(!$data['price']) $this->error("价格为必填!");
			$data['start_date']=strtotime(input('start_date'));
			if($data['start_date']<strtotime(date('Y-m-d'))){
				$this->error("开始时间有误!");
			}
			$data['end_date']=strtotime(input('end_date'));
			if($data['end_date']<strtotime(date('Y-m-d')) || $data['end_date']<=$data['start_date']){
				$this->error("到期时间有误!");
			}
			$data['des']=serialize(input('des/a'));
			$file = request()->file('image');
			if (!empty($file)) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['image'] = $info->getSaveName();
				}
			}
			$ids=input("ids/a");
			$num=input("num/a");
			$option=array();
			if($ids) {
				foreach ($ids as $k => $v) {
					$option[$k]['id'] = $v;
					if ($num[$k] <= 0) {
						$this->error("填写数量有误!");
					}
					$option[$k]['num'] = $num[$k];
				}
			}
			$key=input("key/a");
			$asd=input("param/a");
			$param=array();
			if($key) {
				foreach ($key as $k => $v) {
					$param[$k]['key'] = $v;
					$param[$k]['value'] = $asd[$k];
				}
				$data['param'] = serialize($param);
			}
			$data['option']=serialize($option);
			$files = request()->file('photoname');
			$images=input('photo/a');
			if (!empty($files)) {
				foreach($files as $file){
					// 移动到框架应用根目录/public/uploads/ 目录下
					$info = $file->move(ROOT_PATH . 'public/static/uploads');
					if($info){
						if($images) {
							array_push($images, $info->getSaveName());
						}else{
							$images[] = $info->getSaveName();
						}
					}
				}
			}
			if($images){
				$data['images']=serialize($images);
			}

			if($id){
				$res = db('package')->where("id='$id'")->update($data);
			}else{
				$res = db('package')->insert($data);
			}
			if($res!==false){
				$this->redirect(url("goods/package"));
				$this->success("操作成功!",url("goods/package"));
			}else{
				$this->error("操作失败!");
			}
		}

		$where['id']=$id;
		$where['delete']=1;
		$p['delete']=1;
		$package=db('hotel_room')->where($p)->select();

		$room=db('package')->where($where)->find();
		if($room['option']){
			$room['option']=unserialize($room['option']);
		}
		if($room['param']){
			$room['param']=unserialize($room['param']);
		}
		if($room['images']) {
			$room['images'] = unserialize($room['images']);
		}
		if($room['start_date']) {
			$room['start_date'] = date("Y-m-d",$room['start_date']);
		}
		if($room['end_date']) {
			$room['end_date'] = date("Y-m-d",$room['end_date']);
		}
		if($room['des']) {
			$room['des'] = unserialize($room['des']);
		}
		$this -> assign("package",$package);
		$this -> assign("type",$p['type']);
		$this -> assign("data",$room);
		return $this->fetch();

	}
	/**
	 *   上下架
	 */
	public function status(){
		$id = input('id');
		if(!$id) $this->error('参数有误');
		$where['id']=$id;
		$where['delete']=1;
		$room=db('hotel_room')->where($where)->find();
		if(!$room) $this->error('记录不存在');
		if($room['status']==0){
			$price=db("hotel_room_price")->where("roomid='$id'")->find();
			if(!$price) $this->error('请先设置价格');
			$res=db('hotel_room')->where($where)->update(array('status'=>1));
		}
		if($room['status']==1){
			$res=db('hotel_room')->where($where)->update(array('status'=>0));
		}
		if(!$res) $this->error('操作失败');
		$this->redirect($_SERVER['HTTP_REFERER']);
	}

	/**
	 *   属性模板
	 */
	public function tpl(){

		return $this->fetch();
	}
	/**
	 *   套餐属性模板
	 */
	public function posttpl(){
		$p['type']= intval(input('type'));
		$p['delete']=1;
		$package=db('hotel_room')->where($p)->select();
		$this -> assign("package",$package);
		return $this->fetch();
	}
	/**
	 *   设置价格
	 */
	public function price(){

		$db_room_price=db('hotel_room_price');
		$id=input('id');
		if(!$id) $this->error('参数有误');
		$max=db('hotel_room_price')->where("roomid='$id'")->max('roomdate');
		if(date('m',$max)==date('m') ||$max==0 ){
			$this->getMonth($id);
		}

		$today=strtotime(date('Y-m-d'));
		$db_room_price->where("roomid='$id' && roomdate<'$today'")->delete();
		$room_price=$db_room_price->where("roomid='$id'")->order("roomdate asc")->paginate(10);
		$this -> assign("list",$room_price);
		return $this->fetch();
	}
	/**
	 *   设置价格
	 */
	public function updateprice(){

		$db_room_price=db('hotel_room_price');
		$id=input('id');
		if(!$id){
			echo 0;exit;
		}
		$max=db('hotel_room_price')->where("id='$id'")->find();
		if(!$max){
			echo 0;exit;
		}
		$type=input("type");
		$num=input("num");
		$max=db('hotel_room_price')->where("id='$id'")->update([$type=>$num]);
		if($max!==false){
			echo 1;exit;
		}else{
			echo 0;exit;
		}
	}
	/**
	 *   获取日历
	 */
	public function getMonth($id,$price1=0,$price2=0,$price3=0,$price4=0,$num=0,$oprice=0){

		$month = date("m");
		$year = date("Y");
		$total_month_day = date('t', strtotime(date("Y-m-d")));
		$room_price=db('hotel_room_price');
		for($i=1;$i<=$total_month_day;$i++) {
			if(strlen($i)==1){
				$i='0'.$i;
			}
			if($i>=date("d")) {
				$data['roomdate'] = strtotime($year . '-' . $month . '-' . $i);
				$data['thisdate'] = $year . '-' . $month . '-' . $i;
				$data['roomid'] = $id;
				$data['price1'] = $price1;
				$data['price2'] = $price2;
				$data['price3'] = $price3;
				$data['price4'] = $price4;
				$data['num'] = $num;
				$data['oprice'] = $oprice;
				$price = $room_price->where($data)->find();
				if (!$price) {
					$room_price->insert($data);
				}
			}
		}
	$next_time= strtotime("+1 Month");
		$next_month=date('m', strtotime("+1 Month"));
		$next_year=date('Y', strtotime("+1 Month"));
		$next_month_day = date('t', $next_time);
		for($j=1;$j<=$next_month_day;$j++) {
			if(strlen($j)==1){
				$j='0'.$j;
			}
			$where['roomdate']=strtotime($next_year.'-'.$next_month.'-'.$j);
			$where['thisdate']=$next_year.'-'.$next_month.'-'.$j;
			$where['roomid']=$id;
			$res=$room_price->where($where)->find();
			if(!$res){
				$room_price->insert($where);
			}
		}
	}
	/**
	 *   删除产品
	 */
	public function del(){
		$id = input('id');
		if(!$id) $this->error('参数有误');
		$where['id']=$id;
		$where['delete']=1;
		$room=db('hotel_room')->where($where)->find();
		if(!$room) $this->error('记录不存在');
		$res=db('hotel_room')->where($where)->update(array('delete'=>0));
		if(!$res) $this->error('操作失败');
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	/**
	 *   删除套餐
	 */
	public function packdel(){
		$id = input('id');
		if(!$id) $this->error('参数有误');
		$where['id']=$id;
		$where['delete']=1;
		$room=db('package')->where($where)->find();
		if(!$room) $this->error('记录不存在');
		$res=db('package')->where($where)->update(array('delete'=>0));
		if(!$res) $this->error('操作失败');
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	/**
	 * 组合套餐列表
	 */
	public function package(){
		$room = db('package');
		$where['delete']=1;
		if(input('type')!=''){
			$where['type']=input('type');
		}
		if(input('status')!=''){
			$where['status']=input('status');
		}
		if(input('keyword')!=''){
			$where['title']=array('like','%'.input('keyword').'%');
		}
		$list = $room->where($where)
				->order("sort desc,id desc")
				->paginate(10);
		$this -> assign("list",$list);
		$this -> assign("status",input('status'));
		$this -> assign("type",input('type'));
		$this->assign('keyword',input('keyword'));
		return $this->fetch();
	}

	/**
	 *   套餐上下架
	 */
	public function packagestatus(){
		$id = input('id');
		if(!$id) $this->error('参数有误');
		$where['id']=$id;
		$where['delete']=1;
		$room=db('package')->where($where)->find();
		if(!$room) $this->error('记录不存在');
		if($room['status']==0){
			$res=db('package')->where($where)->update(array('status'=>1));
		}
		if($room['status']==1){
			$res=db('package')->where($where)->update(array('status'=>0));
		}
		if(!$res) $this->error('操作失败');
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
}